﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace DefensiveShield
{
    /// <summary>
    /// Логика взаимодействия для FileSettings.xaml
    /// </summary>
    public partial class FileSettings : Window
    {
        public FilePassport Passport;
        public FileSettings(OsSession session, string fileName)
        {
            InitializeComponent();
            GroupRights.ItemsSource = OtherRights.ItemsSource = ExclusiveRights.ItemsSource =
                    (from object rights in Enum.GetValues(typeof(Rights)) select ((Rights) rights).ToRussianString());
            Passport = OS.Files[fileName];
            GroupRights.SelectedIndex = (int) Passport.GroupRights;
            OtherRights.SelectedIndex = (int) Passport.OtherRights;
            Mandat.ItemsSource = (from object mandat in Enum.GetValues(typeof(Secrecy)) select ((Secrecy) mandat).ToRussianString());
            Mandat.SelectedIndex = (int) Passport.SecrecyNum;
            UserBox.ItemsSource = OS.Users.Keys;
            if (session.UserName != OS.AdministratorName)
            {
                dnogrid.Visibility = Visibility.Hidden;
            }
        }

        private void UserBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            var comboBoxItem = UserBox.SelectedItem as string;
            if (comboBoxItem == null) return;
            var curUser = comboBoxItem;
            if (!Passport.ExclusiveUserAccessRights.ContainsKey(curUser))
            {
                ExclusiveRights.SelectedIndex = -1;
                return;
            }
            ExclusiveRights.SelectedIndex = (int) Passport.ExclusiveUserAccessRights[curUser];
        }

        private void ExclusiveRights_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            var comboBoxItem = UserBox.SelectedItem as string;
            if (comboBoxItem == null) return;
            var curUser = comboBoxItem;
            if (ExclusiveRights.SelectedIndex == -1)
            {
                Passport.ExclusiveUserAccessRights.Remove(curUser);
                return;
            }
            Passport.ExclusiveUserAccessRights[curUser] = (Rights) ExclusiveRights.SelectedIndex;
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            OS.LogMessage(OS.AdministratorName, "Setting changed", Passport.FileName);
            OS.Files[Passport.FileName] = Passport;
            this.Close();
        }

        private void OtherRights_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            Passport.OtherRights = (Rights) OtherRights.SelectedIndex;
        }

        private void GroupRights_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            Passport.GroupRights = (Rights)GroupRights.SelectedIndex;
        }

        private void Mandat_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            Passport.SecrecyNum = (Secrecy) Mandat.SelectedIndex;
        }
    }
}
